package com.peppa.petshop.controller;


import com.peppa.petshop.entity.Good;
import com.peppa.petshop.service.CategoryService;
import com.peppa.petshop.service.GoodService;
import com.peppa.petshop.vo.DataVO;
import com.peppa.petshop.vo.GoodVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Controller
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired(required=true)
    private CategoryService categoryService;

    @ResponseBody
    @RequestMapping(value = "/list")
    public DataVO<GoodVO> categoryList(HttpServletRequest request, Integer page, Integer limit){
        HttpSession session = request.getSession();
        session.setAttribute("category",categoryService.categoryList(1,20));
        return goodService.goodList( page, limit);
    }

    @RequestMapping(value = "/list1")
    public ModelAndView categoryList1(Integer page, Integer limit){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("manage/good");
        return modelAndView;
    }


    @GetMapping(value = "/del")
    public String del(Integer id){
        System.out.println("删除宠物");
        goodService.deleteById(id);
        return "redirect:/index.html";
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public String add(@RequestBody Good good){

        System.out.println(good);
        System.out.println("添加新宠");
        goodService.addGood(good);
        return "redirect:/index.html";
    }

    @GetMapping(value = "/edit")
    public String edit(Integer goodId, String goodName, BigDecimal price, String introduction){
        System.out.println("编辑宠物");
        System.out.println(goodId+" \n"+goodName+"\n"+price+"\n"+introduction);
        goodService.editById(goodId,goodName,price,introduction);
        return "redirect:/index.html";
    }
}

